package byx.container.annotation.extension.impl;

import byx.container.annotation.annotation.Order;
import byx.container.annotation.annotation.Singleton;
import byx.container.annotation.extension.core.AnnotationMetadata;
import byx.container.annotation.extension.core.ClassPostProcessor;
import byx.container.annotation.extension.core.ClassPostProcessorContext;
import byx.container.core.ObjectFactory;
import byx.container.core.RegisterInfo;

/**
 * 处理Singleton注解
 */
@Order(Integer.MAX_VALUE)
public class SingletonProcessor implements ClassPostProcessor {
    @Override
    public ObjectFactory<?> postProcess(ClassPostProcessorContext ctx) {
        RegisterInfo registerInfo = ctx.getRegisterInfo();
        ObjectFactory<?> factory = registerInfo.getFactory();
        AnnotationMetadata annotationMetadata = ctx.getAnnotationMetadata();

        if (!annotationMetadata.isAnnotationPresent(Singleton.class)) {
            return factory.singleton();
        }

        Singleton singleton = annotationMetadata.getAnnotation(Singleton.class);
        if (singleton.value()) {
            return factory.singleton();
        }

        return factory;
    }
}
